﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.SessionState;

namespace SYS.Web.HttpHandler
{
    public class StaticFileHandler : IHttpHandler, IRequiresSessionState, IRouteHandler
    {
        public RequestContext RequestContext { get; set; }

        public StaticFileHandler(RequestContext requestContext)
        {
            this.RequestContext = requestContext;
        }
        public bool IsReusable
        {
            get { throw new NotImplementedException(); }
        }

        public void ProcessRequest(HttpContext context)
        {
            var controllerId = RequestContext.RouteData.GetRequiredString("controllerId");
            IController controller = null;
            IControllerFactory factory = null;
            try
            {
                factory = ControllerBuilder.Current.GetControllerFactory();
                controller = factory.CreateController(RequestContext, controllerId);
                if (controller != null)
                {
                    controller.Execute(RequestContext);
                }
            }
            finally
            {
                factory.ReleaseController(controller);
            }
        }

        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            throw new NotImplementedException();
        }
    }
}